<?php
/**
 * Created by PhpStorm.
 * User: Happy
 * Date: 2016/5/31 0031
 * Time: 8:26
 */
class yunpian{
 // private   $apikey='6f49306a037e2f9ebc03a0965b25749f';
 // private   $tplid='1381201';//模板id  默认为验证码模板
   private   $apikey='b5ea1186074ca750671b5cdb55090fd7';
   private    $tplid='1675924';//模板id  默认为验证码模板
    /*
     * 	【柔怡花絮美容】尊敬的#customer#,您的#service#已经完成。欢迎您下次光临！
     *
     * */
  //发送手机验证码
  public  function  send_code($mobile,$code=''){
      $ch = curl_init();

      /* 设置验证方式 */

      curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

      /* 设置返回结果为流 */
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

      /* 设置超时时间*/
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);

      /* 设置通信方式 */
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      $data=array('tpl_id'=>$this->tplid,'tpl_value'=>('#code#').'='.urlencode($code).'&'.urlencode('#app#').'='.urlencode('发送'),'apikey'=>$this->apikey,'mobile'=>$mobile);

      $json_data =$this-> tpl_send($ch,$data);
      $array = json_decode($json_data,true);
      curl_close($ch);
      return $array;
  }

   //发送配置的信息
    public   function  send_msg($data=array()){

        $ch = curl_init();

        /* 设置验证方式 */

        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json_data =$this-> tpl_send($ch,$data);
        $array = json_decode($json_data,true);
        curl_close($ch);
        return $array;
    }
    //获取模板内容
    public  function  get_tpl_content($data=array()){

        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json_data =$this-> tpl_get($ch,$data);
        $array = json_decode($json_data,true);
        curl_close($ch);
        return $array;
    }
    //创建模板内容
    public  function   create_tpl($data=array()){

        return $this->send_post_request('https://sms.yunpian.com/v2/tpl/add.json',$data);
    }

   //获得账户
    function get_user($ch,$apikey){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
        return curl_exec($ch);
    }
    //发送
    function send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
    //模板发送
    function tpl_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }

     function  tpl_get($ch,$data){
         curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/tpl/get.json');
         curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
         return curl_exec($ch);
     }

    function  send_post_request($url='',$data=array()){
        $ch = curl_init();
        /* 设置验证方式 */
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

        /* 设置返回结果为流 */
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        /* 设置超时时间*/
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);

        /* 设置通信方式 */
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $json_data=  curl_exec($ch);

        $array = json_decode($json_data,true);
        curl_close($ch);
        return $array;
    }

    function voice_send($ch,$data){
        curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        return curl_exec($ch);
    }
}
